
package jarvis.fs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.Directory;

/**
 * 
 * Function: The super class of feature selection measures 
 * @author Jarvis.Guo
 *
 */
public abstract class FeatureSelection {

	protected IndexReader indexReader;
	
	/**
	 * Get the features with term's text and its score
	 * @return
	 */
	abstract public List<ComparableTerm> getFeatures();
	
	/**
	 * Based on the lucene index model
	 * @param dir
	 */
	public FeatureSelection(Directory dir)
	{
		try{
			this.indexReader = IndexReader.open(dir);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
	
	/**
	 * Get the top n feature terms
	 * @param n
	 * @return
	 */
	public List<String> getTopNFeature(int n)
	{
		List<ComparableTerm> list = getFeatures();//get features
		Collections.sort(list);//sort based on the score
		int max = list.size()>n?n:list.size();
		List<String> result = new ArrayList<String>();
		for(int i=0;i<max;i++)//get the top n max scored terms
		{
			result.add(list.get(i).getTermText());
		}
		return result;
	}
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		IndexReader reader = null;
		TermEnum te = reader.terms();
		TermDocs td = reader.termDocs();
		

	}
	
	/**
	 * Close the compution, indexReader closed
	 *
	 */
	public void close()
	{
		try{
			indexReader.close();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

}
